<template>
  <div>
    <el-form
      label-width="100px"
      class="demo-company"
    >
      <el-form-item label="公司邮箱号" prop="userName" class="input">
        <el-input v-model="userName" placeholder="请输入公司的邮箱号" />
        <i class="iconfont icon-mail icon" />
        <div class="box">
          <div v-show="mailState" class="error-msg">{{ hintMail }}</div>
        </div>
      </el-form-item>
      <el-form-item label="邮箱验证码" prop="code" class="mailbox input">
        <el-input v-model="code" placeholder="请输入邮箱验证码" />
        <button type="button" class="mailbox-btn" :disabled="!canSend" @click="getCode" v-text="content">邮箱验证码</button>
        <i class="iconfont icon-key icon" />
        <div class="box">
          <div v-show="codeState" class="error-msg">{{ hintCode }}</div>
        </div>
      </el-form-item>
    </el-form>
    <button class="business" @click="next">下一步</button>
  </div>
</template>
<script>
import { emailReg } from '@/js/regex'
import { captcha } from '@/api/common'
import { countdown } from '@/util/countdown'
import { checkUserName } from '@/api/register'
import { sendEmail, next } from '@/api/register/company'
export default {
  name: 'FirstCompanyRegister',
  data() {
    return {
      content: '发送验证码', // 按钮里显示的内容
      totalTime: 60,
      canSend: true,
      first: true,
      clock: undefined,
      codeForm: {},
      userName: undefined,
      code: undefined,
      uuid: undefined,
      hintMail: '',
      hintCode: '',
      codeState: false,
      mailState: false
    }
  },
  methods: {
    async getCode() {
      if (await this.validUserName()) {
        captcha().then(response => {
          this.codeForm.uuid = response.message
          this.codeForm.code = response.data
          this.sendCode()
        })
      }
    },
    async validUserName() {
      if (!this.userName) {
        this.hintMail = '请先填写邮箱'
        this.mailState = true
        return false
      }
      if (!emailReg.test(this.userName)) {
        this.hintMail = '邮箱格式不正确'
        this.mailState = true
        return false
      }
      const res = await checkUserName(this.userName)
      if (!res.data) {
        this.hintMail = `邮箱 "${this.userName}" 已被注册`
        this.mailState = true
        return false
      }
      return true
    },
    // 企业注册点击下一步
    async next() {
      if (await this.validUserName()) {
        if (!this.send) {
          this.hintCode = '请先获取验证码'
          this.codeState = true
          this.mailState = false
          return
        }
        if (!this.code) {
          this.hintCode = '请先填写验证码'
          this.codeState = true
          this.mailState = false
          return
        }
        const { userName, uuid, code } = this
        next({ userName, uuid }, code).then(res => {
          if (res.data) {
            this.$emit('setCodeForm', {
              uuid: res.message,
              userName
            })
            this.$emit('next')
          }
        })
      }
    },
    sendCode() {
      if (!this.canSend) {
        return
      }
      countdown(this)
      sendEmail(this.codeForm, this.userName).then(response => {
        if (response.data) {
          this.codeForm = {}
          this.uuid = response.message
          this.send = response.data
        }
      }).catch(() => {
        this.send = false
        window.clearInterval(this.clock)
        this.content = '重新发送验证码'
        this.totalTime = 60
        this.canSend = true
      })
    }
  }
}
</script>
<style scoped lang="scss">
.registered {
  padding-top: 88px;
  padding-bottom: 87px;
  background: #f5f5f5;
  .registered-content {
    background: #ffffff;
    .contents {
      display: flex;
      justify-content: space-between;
      height: 600px;
      .left {
        padding-top: 71px;
        margin-left: 90px;
        .icon{
          position: absolute;
          left: 14px;
          font-size: 22px;
          top: 2px;
        }
        .box{
          height: 28px;
          line-height: 28px;
        }
        .error-msg{
          text-align: left;
          padding-left: 0px;
          color: #f56c6c;
        }
        .business{
          width: 400px;
          height: 42px;
          background: #FF3B30;
          color: #ffffff;
          text-align: center;
          line-height: 42px;
          cursor: pointer;
          margin-left: 100px;
          border-radius: 3px;
          font-size: 18px;
        }
        .check{
          margin-top: 18px;
          margin-left: 100px;
          font-size: 12px;
          .clause{
            color: #FF3B30;
          }
        }
      }
      .right {
        .registered-img{
          padding: 114px 40px 0 0;
        }
      }
    }
  }
  .mailbox{
      position: relative;
      .mailbox-btn{
          width: 92px;
          height: 42px;
          background: #F1F1F1;
          color: #8E8E93;
          line-height: 42px;
          text-align: center;
          position: absolute;
          top: 0;
          right: 0;
          cursor: pointer;
      }
  }
  .input{
    margin-bottom: 0;
  }
  /deep/ .registered-content{
    .el-input{
      width: 400px;
      height: 42px;
      .el-input__inner{
        height: 42px;
        padding-left: 47px;
      }
    }
  }
}
</style>
